package cn.pli.bike.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.framemodule.base.BaseActivityPresenter;
import com.framemodule.base.netstatus.NetUtils;
import com.framemodule.utils.CommonUtils;
import com.framemodule.utils.Logger;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.bingoogolapple.androidcommon.adapter.BGAOnItemChildClickListener;
import cn.bingoogolapple.androidcommon.adapter.BGAOnRVItemClickListener;
import cn.pli.bike.App;
import cn.pli.bike.R;
import cn.pli.bike.bean.BadErrBean;
import cn.pli.bike.bean.EventAction;
import cn.pli.bike.bean.HttpResult;
import cn.pli.bike.bean.ProblemBean;
import cn.pli.bike.model.UserModel;
import cn.pli.bike.model.http.ApiException;
import cn.pli.bike.model.subscribers.ProgramSubscriber;

/**
 * 具体问题
 */

public class ProblemDetailActivity extends BaseActivityPresenter<ProblemDetailDelegate> implements View.OnClickListener {
    private String title;
    private int type;
    public static final int REQUEST_CODE = 1;
    String lockNumber;              //锁编号
    String illegalType;             //类型
    String illegalDescription;      //描述



    @Override
    protected void initModel() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //
        EventBus.getDefault().register(this);
        if (getIntent().hasExtra("title")) {
            title = getIntent().getStringExtra("title");
            viewDelegate.setTvTitle(title);
        }
        if (getIntent().hasExtra("type")) {
            type = getIntent().getIntExtra("type", -1);
        }
        if (type == -1) {
            finish();
        }
        getData(type);

        viewDelegate.getProblemTypeAdapter().setOnItemChildClickListener(new BGAOnItemChildClickListener() {
            @Override
            public void onItemChildClick(ViewGroup parent, View childView, int position) {
                viewDelegate.getProblemTypeAdapter().setSelectPosition(position);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.bt_commit:
                String lockNumber = ((EditText) viewDelegate.get(R.id.et_input_bike_code)).getText().toString().trim();              //锁编号
                String illegalType = viewDelegate.getProblemTypeAdapter().getData().get(viewDelegate.getProblemTypeAdapter().getSelectPosition()).getTypeCode();             //类型
                String illegalDescription = viewDelegate.getEtDetail().getText().toString().trim();      //描述
                report(lockNumber, illegalType, illegalDescription);

                break;
            case R.id.iv_qr_scan_logo:

                Intent intent = new Intent(this, CaptureActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
                break;
        }

    }

    /**
     * 获取问题列表
     */
    private void getData(int type) {
        switch (type) {
            case CallCenterActivity.REPORT_FAULT:
                UserModel.goFaultReport(new ProgramSubscriber<HttpResult<List<BadErrBean>>>() {
                    @Override
                    public void onNext(HttpResult<List<BadErrBean>> listHttpResult) {
                        viewDelegate.getProblemTypeAdapter().setData(listHttpResult.getData());
                    }
                });
                break;
            case CallCenterActivity.REPORT_ILLDAGAL:
                UserModel.goIllegalParking(new ProgramSubscriber<HttpResult<List<BadErrBean>>>() {
                    @Override
                    public void onNext(HttpResult<List<BadErrBean>> listHttpResult) {
                        viewDelegate.getProblemTypeAdapter().setData(listHttpResult.getData());
                    }
                });
                break;
        }


    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        viewDelegate.setOnClickListener(this, R.id.iv_title_back, R.id.bt_commit, R.id.iv_qr_scan_logo);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleWeb(Message message) {
        switch (message.what) {
            case EventAction.ACTION_COMMON_PROBLEM:

                break;

        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        /**
         * 处理二维码扫描结果
         */
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                String qrresult = bundle.getString(CodeUtils.RESULT_STRING);
                String lockNum = "";
                if (CommonUtils.isEmpty(qrresult)) {
                    viewDelegate.toast("扫描二维码失败");
                    return;
                }
                if (qrresult.contains("=")) {
                    String[] results = qrresult.split("=");
                    lockNum = results[results.length - 1];
                } else {
                    lockNum = qrresult;
                }

                ((EditText) viewDelegate.get(R.id.et_input_bike_code)).setText(lockNum);
            }
        }
    }

    /**
     * 上报故障
     *
     * @param lockNumber
     * @param illegalType
     * @param illegalDescription
     */
    private void report(String lockNumber, String illegalType, String illegalDescription) {
        if (CommonUtils.isEmpty(lockNumber) && CommonUtils.isEmpty(illegalType) && CommonUtils.isEmpty(illegalDescription)) {
            viewDelegate.toast(getString(R.string.report_problem_can_not_empet));
        }

        Map<String, String> map = new HashMap<>();

        map.put("longitude", App.getApp().amapLocation.getLongitude() + "");
        map.put("latitude", App.getApp().amapLocation.getLatitude() + "");

        switch (type) {
            case CallCenterActivity.REPORT_FAULT:
                if (CommonUtils.isNotEmpty(lockNumber)) {
                    map.put("lockNumber", lockNumber);
                }
                if (CommonUtils.isNotEmpty(lockNumber)) {
                    map.put("faultType", illegalType);
                }
                if (CommonUtils.isNotEmpty(lockNumber)) {
                    map.put("faultDescription", illegalDescription);
                }


                UserModel.faultReport(new ProgramSubscriber<HttpResult<String>>() {
                    @Override
                    public void onNext(HttpResult<String> listHttpResult) {
                        viewDelegate.toast(listHttpResult.getMsg());

                        finish();
                    }

                    @Override
                    public void onErr(ApiException e) {
                        super.onErr(e);
                        viewDelegate.toast(e.getMsg());
                    }
                }, map);
                break;
            case CallCenterActivity.REPORT_ILLDAGAL:
                if (CommonUtils.isNotEmpty(lockNumber)) {
                    map.put("lockNumber", lockNumber);
                }
                if (CommonUtils.isNotEmpty(lockNumber)) {
                    map.put("illegalType", illegalType);
                }
                if (CommonUtils.isNotEmpty(lockNumber)) {
                    map.put("illegalDescription", illegalDescription);
                }


                UserModel.illegalParking(new ProgramSubscriber<HttpResult<String>>() {
                    @Override
                    public void onNext(HttpResult<String> listHttpResult) {
                        viewDelegate.toast(listHttpResult.getMsg());

                        finish();
                    }

                    @Override
                    public void onErr(ApiException e) {
                        super.onErr(e);
                        viewDelegate.toast(e.getMsg());
                    }
                }, map);
                break;
        }


    }


}
